package com.qianbei.common.loading;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;

import java.util.ArrayList;

public class GuideActivity extends BaseActivity implements OnClickListener {
    private Context mContext = this;
    private ViewPager viewPager;
    public ArrayList<Object> viewPagerSubList;// ViewPager每一个页面列表
    // 底部小点图片
    private ImageView[] dots;
    // 记录当前选中位置
    private int currentIndexDots;
    private LinearLayout customGalleryDirectionLayout;
    private Button mBtGoHome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide);
        initLucency();

        mBtGoHome = (Button) findViewById(R.id.bt_go_home);
        mBtGoHome.setOnClickListener(this);

        initViewPager();

        customGalleryDirectionLayout = (LinearLayout) findViewById(R.id.customGalleryDirectionLayout);
        addGalleryDirectionImageView(customGalleryDirectionLayout);
    }

    private void initLucency() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | layoutParams.flags);
        }
    }

    /**
     * 初始化ViewPager(广告位)
     */
    private void initViewPager() {
        viewPager = (ViewPager) findViewById(R.id.myViewPager);

        viewPagerSubList = new ArrayList<Object>();
        initViewPagerSubList();
        // initBusViewPagerSubList();
        GuideViewPagerAdapter guideViewPagerAdapter = new GuideViewPagerAdapter(
                viewPagerSubList);

        viewPager.setAdapter(guideViewPagerAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setPageMargin(0);// 设置viewpager每个页卡的间距，与gallery的spacing属性类似
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    private void initViewPagerSubList() {
        LinearLayout.LayoutParams imageViewLayoutParams = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        ImageView iv = new ImageView(mContext);
        iv.setLayoutParams(imageViewLayoutParams);
        iv.setBackgroundResource(R.drawable.iv_launch1);
        viewPagerSubList.add(iv);
        iv = new ImageView(mContext);
        iv.setLayoutParams(imageViewLayoutParams);
        iv.setBackgroundResource(R.drawable.iv_launch2);
        viewPagerSubList.add(iv);
    }

    /**
     * 生成gallery指示器布局
     */
    private void addGalleryDirectionImageView(
            LinearLayout galleryDirectionLayout) {
        int dots_count = viewPagerSubList.size();
        dots = new ImageView[dots_count];
        for (int i = 0; i < dots_count; i++)

        {
            dots[i] = new ImageView(mContext);
            dots[i].setImageResource(R.drawable.round_unselected);
            dots[i].setScaleType(ScaleType.FIT_CENTER);
            dots[i].setTag(i);

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT, 1);

            dots[i].setLayoutParams(lp);
            galleryDirectionLayout.addView(dots[i]);

        }
        currentIndexDots = 0;
        dots[currentIndexDots]
                .setImageResource(R.drawable.round_selected);
    }

    /**
     * ViewPager监听
     */
    class MyOnPageChangeListener implements OnPageChangeListener {
        public void onPageScrollStateChanged(int arg0) {
        }

        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        public void onPageSelected(final int position) {
            setCurrentDot(position);
            if (position == 0) {
                mBtGoHome.setVisibility(View.GONE);
                customGalleryDirectionLayout.setVisibility(View.VISIBLE);
            } else if (position == 1) { // TODO待定
                customGalleryDirectionLayout.setVisibility(View.GONE);
                mBtGoHome.setVisibility(View.VISIBLE);

            }
        }
    }

    private void setCurrentDot(int position) {
        if (position < 0 || position > viewPagerSubList.size() - 1
                || currentIndexDots == position) {
            return;
        }

        dots[position]
                .setImageResource(R.drawable.round_selected);
        dots[currentIndexDots]
                .setImageResource(R.drawable.round_unselected);

        currentIndexDots = position;
    }

    @Override
    public void onClick(View arg0) {
        SharedPreferences userInfo = getSharedPreferences("user_info", 0);
        switch (arg0.getId()) {

            case R.id.bt_go_home:
                userInfo.edit().putBoolean("isShowGuide", false).commit();
                finish();
                break;
        }

    }
}
